package com.bw.finance.controller;


import com.bw.finance.conf.ResponseResult;
import com.bw.finance.eneity.user.EbankBorrower;
import com.bw.finance.eneity.user.EbankInvestor;
import com.bw.finance.eneity.user.res.ZhuceLogin;
import com.bw.finance.service.IEbankInvestorService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lizhengqian
 * @since 2020-08-17
 */
@RestController
@RequestMapping("/investor")
public class EbankInvestorController {

    @Resource
    private IEbankInvestorService iEbankInvestorService;

    private static final Logger log= LoggerFactory.getLogger(EbankInvestorController.class);

//    投资人列表
    @PostMapping("list/{pagenum}/{pagesize}")
    public ResponseResult invlist(@PathVariable int pagenum , @PathVariable int pagesize, @RequestBody EbankInvestor ebankInvestor){
        return iEbankInvestorService.invlist(pagenum,pagesize,ebankInvestor);
    }
//    状态修改 update
    @PostMapping("update/{id}/{val}")
    public ResponseResult update01(@PathVariable int id , @PathVariable int val){
        return iEbankInvestorService.update01(id,val);
    }

    @GetMapping("/sendcode/{tel}")
    public ResponseResult sendcode(@PathVariable String tel){
        log.info("验证码发送开始");
        ResponseResult result = ResponseResult.SUCCESS();
        try {
            result=iEbankInvestorService.sendcode(tel);
        }catch (Exception e){
            log.error("验证码发送错误,{}",e.toString());
            result.setSuccess(false);
            result.setMessage("验证码发送失败"+e.toString());
        }
        log.info("验证码发送结束");
        return result;
    }

    @GetMapping("/sendemailcode/{tel}")
    public ResponseResult sendemailcode(@PathVariable String tel){
        log.info("验证码发送开始");
        ResponseResult result = ResponseResult.SUCCESS();
        try {
            result=iEbankInvestorService.sendemailcode(tel);
        }catch (Exception e){
            log.error("验证码发送错误,{}",e.toString());
            result.setSuccess(false);
            result.setMessage("验证码发送失败"+e.toString());
        }
        log.info("验证码发送结束");
        return result;
    }

    @PostMapping("/zhuce")
    public ResponseResult zhuce(@RequestBody ZhuceLogin zhuceLogin){
        log.info("注册用户开始");
        ResponseResult result = ResponseResult.SUCCESS();
        try {
            result=iEbankInvestorService.zhuce(zhuceLogin);
        }catch (Exception e){
            log.error("注册用户错误,{}",e.toString());
            result.setSuccess(false);
            result.setMessage("注册用户失败"+e.toString());
        }
        log.info("注册用户结束");
        return result;
    }

    @PostMapping("/peocheck")
    public ResponseResult peocheck(@RequestBody EbankInvestor investor){
        ResponseResult result = ResponseResult.SUCCESS();
        log.info("身份证认证开始");
        try {
            EbankInvestor ebankInvestor = iEbankInvestorService.getById(investor.getId());
            ebankInvestor.setIdNum(investor.getIdNum());
            iEbankInvestorService.saveOrUpdate(ebankInvestor);
            result.setMessage("认证成功");
        }catch (Exception e){
            log.info("身份证认证失败!"+e.toString());
            result.setMessage("认证失败!"+e.toString());
            result.setSuccess(false);
        }
        log.info("身份证认证结束");
        return result;
    }

    @PostMapping("/bankcheck")
    public ResponseResult bankcheck(@RequestBody EbankInvestor investor){
        ResponseResult result = ResponseResult.SUCCESS();
        log.info("银行账户认证开始");
        try {
            EbankInvestor ebankInvestor = iEbankInvestorService.getById(investor.getId());
            ebankInvestor.setBank(investor.getBank());
            ebankInvestor.setAccount(investor.getAccount());
            ebankInvestor.setTokenPwd(investor.getTokenPwd());
            iEbankInvestorService.saveOrUpdate(ebankInvestor);
            result.setMessage("认证成功");
        }catch (Exception e){
            log.info("银行账户认证失败!"+e.toString());
            result.setMessage("认证失败!"+e.toString());
            result.setSuccess(false);
        }
        log.info("银行账户认证结束");
        return result;
    }

    @GetMapping("/findone/{id}")
    public ResponseResult findone(@PathVariable Long id){
        ResponseResult result = ResponseResult.SUCCESS();
        log.info("个人信息查询开始");
        try {
            EbankInvestor investor = iEbankInvestorService.getById(id);
            result.setResult(investor);
        }catch (Exception e){
            log.info("个人信息查询失败!"+e.toString());
            result.setMessage("查询失败!"+e.toString());
            result.setSuccess(false);
        }
        log.info("个人信息查询结束");
        return result;
    }

    @PostMapping("/findname/id")
    public ResponseResult findname(@PathVariable Long id){
        ResponseResult result = ResponseResult.SUCCESS();
        try {
            EbankInvestor byId = iEbankInvestorService.getById(id);
            result.setResult(byId.getFullName());
        }catch (Exception e){
            result.setMessage("认证失败!"+e.toString());
            result.setSuccess(false);
        }
        return result;
    }
}
